package esa.android;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;

public class JSONDataFetchers {

	private static JSONDataFetchersContext context;

	private static final String SERVER_URL ="http://oulu-lounas.appspot.com";
	
	public static void initialize(JSONDataFetchersContext context) {
		JSONDataFetchers.context = context;
	}

	public interface JSONDataFetchersContext {
		public void showProgressDialog(int messageId);
		public void hideProgressDialog();
		public void showErrorDialog();
		public void showMessage(int messageId);
	}
	
	public static abstract class FetchJSONDataTask extends AsyncTask<Object, Void, JSONArray> {

		@Override
		protected void onPreExecute() {
			context.showProgressDialog(-1);
		}

		@Override
		protected JSONArray doInBackground(Object... v) {
			return fetchJSONResult(v);
		}

		@Override
		protected void onPostExecute(JSONArray jsonResult) {
			context.hideProgressDialog();
			if (jsonResult != null) {
				processJSONResult(jsonResult);
			} else {
				context.showErrorDialog();
			}
		}

		protected abstract void processJSONResult(JSONArray jsonResult);

		protected abstract JSONArray fetchJSONResult(Object... v);

	}

	public static class FetchLunchData extends FetchJSONDataTask {

		private static final String FETCH_LUNCH_URL = SERVER_URL+"/lunchmenu/fetchMenu";

		@Override
		protected void onPreExecute() {
			int dayId=-1;
			switch (getCurrentDay()) {
			case 0:
				dayId = R.string.day_1;
				break;
			case 1:
				dayId = R.string.day_2;
				break;
			case 2:
				dayId = R.string.day_3;
				break;
			case 3:
				dayId = R.string.day_4;
				break;
			case 4:
				dayId = R.string.day_5;
				break;
			}
			context.showProgressDialog(dayId);
			context.showMessage(dayId);
		}

		
		// monday to friday = 0-4
		protected int getCurrentDay() {
			int d = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
			switch (d) {
			case Calendar.MONDAY:
				return 0;
			case Calendar.TUESDAY:
				return 1;
			case Calendar.WEDNESDAY:
				return 2;
			case Calendar.THURSDAY:
				return 3;
			case Calendar.FRIDAY:
				return 4;

			}
			return 0;
		}

		@Override
		protected JSONArray fetchJSONResult(Object... v) {
			int d = getCurrentDay();
			Collection<?> selectedPlaces = (Collection<?>) v[0];
			JSONHttpRequest c = new JSONHttpRequest(FETCH_LUNCH_URL);
			return c.doPOSTRequest("currentDay", d, "day", d, "selected", selectedPlaces);
		}

		@Override
		protected void processJSONResult(JSONArray jsonResult) {
			Collection<Map<String, Object>> menus = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < jsonResult.length(); i++) {
				//Log.i("test",jsonResult.toString());
				JSONObject o = jsonResult.optJSONObject(i);
				Log.i("LunchMenu",o.toString());
				JSONObject place=null;
				try {
					place = o.getJSONObject("place");
				} catch (JSONException e) {
					continue;
				}
				
				Map<String, Object> dayMenu = new HashMap<String,Object>();
				menus.add(dayMenu);

				dayMenu.put("name", place.optString("name"));
				dayMenu.put("placeId", place.optString("placeId"));
				dayMenu.put("homePageUrl", place.optString("homePageUrl"));
				
				try {
					JSONObject location = place.getJSONObject("location");
					dayMenu.put("longitude", location.optString("longitude"));
					dayMenu.put("latitude", location.optString("latitude"));

				} catch (JSONException e) {

				}
				
				Collection<String> entries = new ArrayList<String>();
				dayMenu.put("menuEntries", entries);

				JSONArray menuEntries = o.optJSONArray("menuEntries");
				for (int j = 0; j < menuEntries.length(); j++) {
					JSONObject menu = menuEntries.optJSONObject(j);
					String name = menu.optString("name");
					if (name != null) {
						entries.add(name);
					}
				}
			}
			Log.i("LunchMenu", "received json object: " + menus);

			updateUI(menus);

		}

		protected void updateUI(Collection<Map<String, Object>> dayMenus) {

		}

	}

	public static class FetchPlacesData extends FetchJSONDataTask {

		private static final String FETCH_PLACES_URL = SERVER_URL+"/lunchmenu/fetchPlaces";

		@Override
		protected JSONArray fetchJSONResult(Object... v) {
			JSONHttpRequest c = new JSONHttpRequest(FETCH_PLACES_URL);
			return c.doPOSTRequest();
		}

		@Override
		protected void processJSONResult(JSONArray jsonResult) {
			
			Collection<Map<String, Object>> places = new ArrayList<Map<String, Object>>();
			
			for (int i = 0; i < jsonResult.length(); i++) {
				
				JSONObject o = jsonResult.optJSONObject(i);

				Map<String, Object> place = new HashMap<String,Object>();
				
				place.put("name", o.optString("name"));
				place.put("placeId", o.optString("placeId"));

				places.add(place);
			}
			Log.i("LunchMenu", "received json object: " + places);

			updateUI(places);
		}

		protected void updateUI(Collection<Map<String, Object>> places) {

		}

	}

}
